package com.mxnet.chongjin.supoman.fragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mxnet.chongjin.supoman.R;
import com.mxnet.chongjin.supoman.activity.AllMachineActivity;
import com.mxnet.chongjin.supoman.activity.MachineInstructionActivity;
import com.mxnet.chongjin.supoman.activity.OnlineRepairActivity;
import com.mxnet.chongjin.supoman.activity.UserLoginActivity;
import com.mxnet.chongjin.supoman.bean.HotMachine;
import com.mxnet.chongjin.supoman.config.Api;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import okhttp3.Call;

/**
 * A simple {@link Fragment} subclass.
 */
public class ServerFragment extends Fragment implements View.OnClickListener {
    private Context mContext;
    private TextView mTextView;
    private FrameLayout mFrameLayout1, mFrameLayout2, mFrameLayouta, mFrameLayoutb, mFrameLayoutc;
    private ImageView mImageViewA, mImageViewB, mImageViewC;
    private TextView mTextViewA, mTextViewB, mTextViewC;
    private List<HotMachine> mList= new Vector<>();
    private SharedPreferences mPreferences;
    private boolean isLogin;

    public ServerFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_server, container, false);
        mContext = this.getActivity();
        mPreferences = mContext.getSharedPreferences("supoman", Context.MODE_PRIVATE);
        isLogin = mPreferences.getBoolean("isLogin", false);

        initView(v);
        initData();
        return v;
    }

    public void initView(View view) {
        mTextView = view.findViewById(R.id.tv_server);
        mTextView.setOnClickListener(this);

        mFrameLayout1 = view.findViewById(R.id.fl_server_1);
        mFrameLayout1.setOnClickListener(this);

        mFrameLayout2 = view.findViewById(R.id.fl_server_2);
        mFrameLayout2.setOnClickListener(this);

        mFrameLayouta = view.findViewById(R.id.fl_server_a);
        mFrameLayouta.setOnClickListener(this);

        mFrameLayoutb = view.findViewById(R.id.fl_server_b);
        mFrameLayoutb.setOnClickListener(this);

        mFrameLayoutc = view.findViewById(R.id.fl_server_c);
        mFrameLayoutc.setOnClickListener(this);

        mImageViewA = view.findViewById(R.id.iv_server_a);
        mTextViewA = view.findViewById(R.id.tv_server_a);

        mImageViewB = view.findViewById(R.id.iv_server_b);
        mTextViewB = view.findViewById(R.id.tv_server_b);

        mImageViewC = view.findViewById(R.id.iv_server_c);
        mTextViewC = view.findViewById(R.id.tv_server_c);
    }

    public void initData() {
        mList = new Vector<>();

        OkHttpUtils.get()
                .url(Api.URL_HEADER + Api.HOT_MACHINE)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        if (!TextUtils.isEmpty(response)) {
                            try {
                                JSONObject jsonObject = new JSONObject(response);
                                boolean isSuccess = jsonObject.optBoolean("success");
                                if (isSuccess) {
                                    JSONArray data = jsonObject.optJSONArray("data");
                                    for (int i = 0; i < data.length(); i++) {
                                        HotMachine hotMachine = new HotMachine();
                                        JSONObject object = data.optJSONObject(i);
                                        hotMachine.setId(object.optString("id"));
                                        hotMachine.setRobotImgUrl(object.optJSONObject("imgResource").optString("url"));
                                        hotMachine.setRobotNo(object.optString("robotNo"));
                                        if (hotMachine != null) {
                                            mList.add(hotMachine);
                                        }
                                    }
                                    if (mList.size() == 0) {
                                    } else if (mList.size() == 1) {
                                        Glide.with(mContext).load(Api.URL_HEADER + Api.GET_IMAGE + mList.get(0).getRobotImgUrl()).into(mImageViewA);
                                        mTextViewA.setText(mList.get(0).getRobotNo());
                                        mFrameLayoutb.setVisibility(View.GONE);
                                        mFrameLayoutc.setVisibility(View.GONE);
                                    } else if (mList.size() == 2) {
                                        Glide.with(mContext).load(Api.URL_HEADER + Api.GET_IMAGE + mList.get(0).getRobotImgUrl()).into(mImageViewA);
                                        mTextViewA.setText(mList.get(0).getRobotNo());
                                        Glide.with(mContext).load(Api.URL_HEADER + Api.GET_IMAGE + mList.get(1).getRobotImgUrl()).into(mImageViewB);
                                        mTextViewB.setText(mList.get(1).getRobotNo());
                                        mFrameLayoutc.setVisibility(View.GONE);
                                    } else {
                                        Glide.with(mContext).load(Api.URL_HEADER + Api.GET_IMAGE + mList.get(0).getRobotImgUrl()).into(mImageViewA);
                                        mTextViewA.setText(mList.get(0).getRobotNo());
                                        Glide.with(mContext).load(Api.URL_HEADER + Api.GET_IMAGE + mList.get(1).getRobotImgUrl()).into(mImageViewB);
                                        mTextViewB.setText(mList.get(1).getRobotNo());
                                        Glide.with(mContext).load(Api.URL_HEADER + Api.GET_IMAGE + mList.get(2).getRobotImgUrl()).into(mImageViewC);
                                        mTextViewC.setText(mList.get(2).getRobotNo());
                                    }
                                } else {
                                    //                                    ToastUtils.showToast(mContext, "");
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.tv_server:
                intent.setClass(mContext, AllMachineActivity.class);
                mContext.startActivity(intent);
                break;
            case R.id.fl_server_1:
                intent.setClass(mContext, AllMachineActivity.class);
                mContext.startActivity(intent);
                break;
            case R.id.fl_server_2:
                if (isLogin) {
                    intent.setClass(mContext, OnlineRepairActivity.class);
                    mContext.startActivity(intent);
                } else {
                    intent.setClass(mContext, UserLoginActivity.class);
                    mContext.startActivity(intent);
                }
                break;
            case R.id.fl_server_a:
                intent.setClass(mContext, MachineInstructionActivity.class);
                intent.putExtra("key", mTextViewA.getText());
                mContext.startActivity(intent);
                break;
            case R.id.fl_server_b:
                intent.setClass(mContext, MachineInstructionActivity.class);
                intent.putExtra("key", mTextViewB.getText());
                mContext.startActivity(intent);
                break;
            case R.id.fl_server_c:
                intent.setClass(mContext, MachineInstructionActivity.class);
                intent.putExtra("key", mTextViewB.getText());
                mContext.startActivity(intent);
                break;
        }
    }

    //    @Override
    //    public void onHiddenChanged(boolean hidden) {
    //        if (!hidden) {
    //            getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
    //        }
    //    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mFrameLayout1 = null;
        mFrameLayout2 = null;
        mFrameLayouta = null;
        mFrameLayoutb = null;
        mFrameLayoutc = null;
        mImageViewA = null;
        mImageViewB = null;
        mImageViewC = null;
        mTextView = null;
        mTextViewA = null;
        mTextViewB = null;
        mTextViewC = null;
        mList.clear();
    }
}
